package com.jlcloud.core.log.config;

import com.jlcloud.core.log.aspect.ApiLogAspect;
import com.jlcloud.core.log.feign.ILogClient;
import com.jlcloud.core.log.logger.JlCloudLogger;
import com.jlcloud.core.log.props.JlCloudRequestLogProperties;
import com.jlcloud.core.launch.log.JlCloudLogLevel;
import com.jlcloud.core.launch.props.JlCloudProperties;
import com.jlcloud.core.launch.props.JlCloudPropertySource;
import com.jlcloud.core.launch.server.ServerInfo;
import com.jlcloud.core.log.aspect.LogTraceAspect;
import com.jlcloud.core.log.event.ApiLogListener;
import com.jlcloud.core.log.event.ErrorLogListener;
import com.jlcloud.core.log.event.UsualLogListener;
import com.jlcloud.core.log.filter.LogTraceFilter;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.core.Ordered;

import jakarta.servlet.DispatcherType;

/**
 * 日志工具自动配置
 *
 * @author By: JlCloud <br>
 * @Package: com.jlcloud <br>
 * @CreateTime: 2024-12-03 21:45 <br>
 * @Copyright: 2021 www.jilianjituan.com Inc. All rights reserved. <br>
 * @Caution 注意：本内容仅限于冀联人力集团内部传阅，禁止外泄以及用于其他的商业目的 <br>
 */
@AutoConfiguration
@ConditionalOnWebApplication
@EnableConfigurationProperties(JlCloudRequestLogProperties.class)
@JlCloudPropertySource(value = "classpath:/jlcloud-log.yml")
public class JlCloudLogToolAutoConfiguration {

	@Bean
	public ApiLogAspect apiLogAspect() {
		return new ApiLogAspect();
	}

	@Bean
	public LogTraceAspect logTraceAspect() {
		return new LogTraceAspect();
	}

	@Bean
	public JlCloudLogger jlcloudLogger() {
		return new JlCloudLogger();
	}

	@Bean
	@ConditionalOnProperty(value = JlCloudLogLevel.REQ_LOG_PROPS_PREFIX + ".trace-log", matchIfMissing = true)
	public FilterRegistrationBean<LogTraceFilter> logTraceFilterRegistration() {
		FilterRegistrationBean<LogTraceFilter> registration = new FilterRegistrationBean<>();
		registration.setDispatcherTypes(DispatcherType.REQUEST);
		registration.setFilter(new LogTraceFilter());
		registration.addUrlPatterns("/*");
		registration.setName("LogTraceFilter");
		registration.setOrder(Ordered.LOWEST_PRECEDENCE);
		return registration;
	}

	@Bean
	@ConditionalOnMissingBean(name = "apiLogListener")
	public ApiLogListener apiLogListener(ILogClient logService, ServerInfo serverInfo, JlCloudProperties jlcloudProperties) {
		return new ApiLogListener(logService, serverInfo, jlcloudProperties);
	}

	@Bean
	@ConditionalOnMissingBean(name = "errorEventListener")
	public ErrorLogListener errorEventListener(ILogClient logService, ServerInfo serverInfo, JlCloudProperties jlcloudProperties) {
		return new ErrorLogListener(logService, serverInfo, jlcloudProperties);
	}

	@Bean
	@ConditionalOnMissingBean(name = "usualEventListener")
	public UsualLogListener usualEventListener(ILogClient logService, ServerInfo serverInfo, JlCloudProperties jlcloudProperties) {
		return new UsualLogListener(logService, serverInfo, jlcloudProperties);
	}

}
